<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_TranslateAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		f_core_Routing::getInstance()->setRequestURL($this->getRequest()->getParameter("href"));

		$this->addToModel("href", $this->getRequest()->getParameter("href"));
		
		$docId = $this->getRequest()->getParameter("docId");
		
		$formValues = $this->getFormValues("form");
		$website = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Website', $docId);
		
		if($website->isATranslation())
		{
			$website = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Website', $docId, $website->getVO());
		}
		
		if(count($formValues) > 0)
		{
			$errors = $this->checkForm($formValues, $docId);

			if(count($errors) == 0)
			{
				$this->saveTranslation($website, $formValues["iso"]);
				$errors["status"] = "success";
			}
			else
			{
				$errors["status"] = "failed";
			}
            
            $this->addToModel("errors", $errors);
		}
        else
        {
            $formValues['iso'] = strtoupper(f_core_Context::getInstance()->getBrowserLanguage());
        }
        
        $this->addToModel("originalIso", f_locale_Manager::getDatasFromIso($website->getLang()));
        $this->addToModel("isoCode", f_locale_Manager::getIsoCodes());
        $this->addToModel("values", $formValues);
        $this->addToModel("website", $website);
        $this->setView('success');
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight("website_Translation");
	}
	
	private function checkForm($formValues, $docId)
	{
		$errors = array();
				
		if(f_document_Provider::getInstance()->getByModelAndUniqueId('website/Website', $docId, $formValues["iso"])->isTranslated())
		{
			$errors["iso"] = $this->getLocale('website.errors.alreadyTranslated');
		}
		
		return $errors;
	}
	
	private function saveTranslation($origWebsite, $destLang)
	{
		$rootNode = m_backoffice_services_RootNodeService::getInstance()->getModuleRootNode("website");				
		
		$rootNodeTranslated = f_document_Provider::getInstance()->getByModelAndUniqueId('backoffice/RootNode', $rootNode->getUniqueId(), $destLang);
		$rootNodeTranslated->setLabel($rootNode->getLabel()." (".$destLang.")")->save();
		
		$websiteTranslated = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Website', $origWebsite->getUniqueId(), $destLang);

		$websiteTranslated	->setAuthor($this->getUser()->getLogin())
							->setLabel($origWebsite->getLabel()." (".strtoupper($destLang).")")
							->setUrl($origWebsite->getUrl()."/".strtolower($destLang))
							->setPublicationstatus("DRAFT")
                            ->setDisplayLang(strtolower($destLang))
							->save();
		
		$rootNodeTranslated->addWebsite($websiteTranslated)->save();
	}
}